function Send_User(){
    for (let i=1; i<=10; i++){
        logd("第", i, "次运行");
        var res = TG_Set();
        if(res){
            return true;
        }
        sleep(1000);
    }
    return false;
}


function TG_Set(){
    var search = desc("Search");
    var result = has(search);
    if (result) {
        click(search);
        logd("点击查询按钮");

        sleep(1000);
        return  Input_data();
    }else {
        toast("没有找到查询按钮");
    }
}

function Input_data(){
    var selectors = text("Search");
    var result1 = has(selectors);
    if (result1) {
        str = "";
        for (let i=0; i<5; i++){
            var num = random(97, 122);
            var c = String.fromCharCode(num);
            str = str + c;
        }
        var result2 = input_text("@"+str);
        if (result2){
            logd("输入:@" + str);
            sleep(2000);
            var title = text("Global search").getOneNodeInfo(500);
            if (title) {
                var user_list = title.parent().nextSiblings();
                user_list[0].click();
                sleep(4000);
                return Is_bot();
            }else {
                toast("没有找到目标用户");
                sleep(500);
                back();
                back();
            }
        }
    }else {
        toast("输入失败");
        var to_back = desc("Go back");
        click(to_back);
        logd("返回至Telegram主界面");
        sleep(1000);
    }
}

function Is_bot(){
    var selectors = text("Message");
    var result1 = has(selectors);
    if (result1) {
        logd("此人为真人");
        clickPoint(205, 853)
        sleep(1000);
        input_text("hello");
        sleep(1000);
        var send = desc("Send");
        var result2 = click(send);
        sleep(1000);
        if (result2) {
            logd("发送信息:hello");
            sleep(3000);
            var ok = text("OK");
            var result3 = has(ok);
            if (result3) {
                click(ok);
                logd("对方已设置隐私权限,点击OK");
                sleep(1000);
            }
            back();
            sleep(500);
            back();
            sleep(1000);
            return true;
        }else{
            toast("发送失败");
            var to_back = desc("Go back");
            click(to_back);
            logd("返回至Telegram主界面");
            sleep(1000);
        }
    }else {
        toast("此人为人机");
        sleep(2000);
        back();
        sleep(500);
        back();

    }
}

// Send_User();